/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.dao.services.ProjectDAOServices;
import model.enetities.NewsCategory;
import model.enetities.Project;
import model.enetities.ProjectCategory;

/**
 *
 * @author Ku81
 */
public class ProjectDAO implements ProjectDAOServices{
    
    private static ProjectDAO projectDAO;

    public static ProjectDAO getInstance() {
        if (projectDAO == null) {
            projectDAO = new ProjectDAO();
        }
        return projectDAO;
    }

    @Override
    public List<Project> getProject() {
    
        List<Project> listProject = new ArrayList<Project>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblProject where isActived = 'True'";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                Project prj = new Project();
                prj.setIdProject(rs.getInt("idProject"));
                prj.setName(rs.getString("name"));
                prj.setStartDate(rs.getDate("startDate"));
                prj.setFinishDate(rs.getDate("finishDate"));
                prj.setDesciptions(rs.getString("descriptions"));
                ProjectCategory prjCtr = ProjectCategoryDAO.getInstance().getProjectCategoryByID(rs.getInt("idProjectCategory"),true);
                prj.setProjectCategory(prjCtr);

                listProject.add(prj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listProject;
        
    }

    @Override
    public Project getProjectByID(int idProject,boolean isActived) {
        Project prj = new Project();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblProject where idProject = ? and isActived = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, idProject);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                prj.setIdProject(rs.getInt("idProject"));
                prj.setName(rs.getString("name"));
                prj.setStartDate(rs.getDate("startDate"));
                prj.setFinishDate(rs.getDate("finishDate"));
                prj.setDesciptions(rs.getString("descriptions"));
                ProjectCategory prjCtr = ProjectCategoryDAO.getInstance().getProjectCategoryByID(rs.getInt("idProjectCategory"),true);
                prj.setProjectCategory(prjCtr);

            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return prj;
    }

    @Override
    public boolean createProject(Project project) {
        boolean isCheck = false;
        try {
 
            Connection conn = ConnectionFactory.getConnection();
            String sql = "insert into tblProject values (?,?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, project.getName());
            pstmt.setDate(2, (Date) project.getStartDate());
            pstmt.setDate(3, (Date) project.getFinishDate());
            pstmt.setString(4, project.getDesciptions());
            pstmt.setInt(5, project.getProjectCategory().getIdProjectCategory());
            pstmt.setBoolean(6, project.isActived());
            
            pstmt.executeUpdate();
            isCheck = true;
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck;
    }

    @Override
    public boolean deleteProject(int idProject) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "Update tblProject Set isActived = 'false' where idProject = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, idProject );
            pstmt.executeUpdate();

            isCheck = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isCheck;
    }

    @Override
    public boolean updateProject(Project project) {
        boolean isCheck = false;
        try {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            Connection con = ConnectionFactory.getConnection();
            String sql = "update tblProject set name = ?, startDate = ?, finishDate = ?, descriptions = ?, idProjectCategory = ?, isActived = ? where idCategory = ? ";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, project.getName());
            pstmt.setDate(2, (Date) project.getStartDate());
            pstmt.setDate(3, (Date) project.getFinishDate());
            pstmt.setString(4, project.getDesciptions());
            pstmt.setInt(5, project.getProjectCategory().getIdProjectCategory());
            pstmt.setBoolean(6, project.isActived());
            pstmt.setInt(7, project.getIdProject());
            pstmt.executeUpdate();
            isCheck = true;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck;
        
        }
    }



    

